var testn;
if(!testn){
	testn =1;
}
var brandFun={
	inits:function(){
		this.ajax();
		this.DetailsFun();//进入详情
		this.DaohangeventFun();
	},
	Interface:{
		goodsDaohang:"http://ww.yc630.com/api/index.php/Buyer/Goods/goodsDaohang",  //商品导航
		brandHomepage:"http://ww.yc630.com/api/index.php/Buyer/Goods/brandHomepage",//品牌首页
		sellerdetail:"http://ww.yc630.com/api/index.php/Buyer/Goods/sellerdetail"   //商家详情
	},
	goodsListObj:{
		people_style:common.Geturl().people_style,
		goods_style:common.Geturl().goods_style,
		brand_id:common.Geturl().brand,
		prodaddress:common.Geturl().prodaddress,
		//seller_id:common.Geturl().seller_id,
		token:token,
		page: testn,
		pagelimit:30
	},
	ajax:function(){
		var people_style=common.Geturl().people_style,
			brand=common.Geturl().brand;
			brandFun.goodsListObj.token=token;
			brandFun.goodsListObj.brand_id=brand;
			//console.log(brandFun.goodsListObj);
			if ((/seller_id/.test(window.location.href))) {
				brandFun.goodsListObj.seller_id=common.Geturl().seller_id;
			}
			// else {
			// 	var o_href=window.location.href;
			// 	window.location.href=o_href+"&seller_id="+;
			// 	brandFun.goodsListObj.seller_id=common.getCookie("sellerid");
			// };
			

		common.ajaxFun(brandFun.Interface.sellerdetail,"POST",brandFun.goodsListObj,brandFun.sellerdetailFun); //商家详情
		
	},
	sellerdetailFun:function(res){
		$("#BrandLogo img").attr("src",res.data.icon_image);//牌照
		$("#SetUpTime").html(res.data.build_time);///注册时间
		$("#registeredCapital").html(res.data.money)//注册资本
		$("#LegalRepresentative").html(res.data.boss)//法人代表
		$("#serviceNum").html(res.data.customer_tel)//客服电话
		//common.setCookie("sellerid",res.data.id);
//console.log(res.data.id)
		people_style=common.Geturl().people_style;
		brand=common.Geturl().brand;
		brandFun.goodsListObj.seller_id=common.getCookie("sellerid");
//console.log(brandFun.goodsListObj)
		if (res.code==0) {
			if ((/seller_id/.test(window.location.href))) {
				brandFun.goodsListObj.seller_id=common.Geturl().seller_id;
				seller_id=common.Geturl().seller_id;

			}else {
				var o_href=window.location.href;
				window.location.href=o_href+"&seller_id="+res.data.id;
				brandFun.goodsListObj.seller_id=res.data.id;
				seller_id=res.data.id;

			};

			common.ajaxFun(brandFun.Interface.goodsDaohang,"POST",{people_style:people_style,token:token,seller_id:seller_id},brandFun.DaohangFun);   //导航
			common.ajaxFun(brandFun.Interface.brandHomepage,"POST",brandFun.goodsListObj,brandFun.goodsListFun);
		   //商品

		};
	},
	DaohangFun:function(res){
		brandFun.initdaohang(res,1);
		brandFun.intiChange();
	},
	changeUrl:function(attrs,attrVal,peoples){
		//console.log(attrs)
		var newGetUrlParam= common.GetUrlParam();
		var origin_href=window.location.href.split("?")[0];
		//newGetUrlParam["pageNumber"]=1;
		var strGetUrlParam="";
		newGetUrlParam[attrs]=attrVal;
		console.log(newGetUrlParam)
		if (peoples==1) {
			strGetUrlParam=attrs+"="+attrVal+"&seller_id="+common.Geturl().seller_id;
		}else {
			$.each(newGetUrlParam,function(i,v){
				strGetUrlParam+=i+"="+v+"&";
			});
			strGetUrlParam=strGetUrlParam.substring(0,strGetUrlParam.length-1);
		};
		window.location.href=origin_href+"?"+strGetUrlParam;
	},
	initdaohang:function(res,yn){
		var Str="";
		var brands=res.data.brands,goodstyle=res.data.goodstyle,peopletype=res.data.peopletype,prodaddress=res.data.shipprovince_address;
		function daohanglist(data,ele,key){
			var Str="";
			if (data!=res.data.peopletype) {
				if (data==null) {
					Str="";
				}else if (data!=null) {
					$.each(data,function(i,v){
						Str+='<span attrId='+i+'  key='+key+'>'+v+'</span>'
					});
				};
				ele.html(Str);
			}else {
				if (yn==1) {
					$.each(res.data.peopletype,function(i,v){
						Str+='<span attrId='+i+'  key='+key+'>'+v+'</span>'
					});
					ele.html(Str);//适应人群
				};
			};
		}
		daohanglist(brands,$("#brands"),"brand")//品牌
		daohanglist(goodstyle,$("#goodstyle"),"goods_style")//风格
		daohanglist(peopletype,$("#peopletype"),"people_style")//适应人群
		daohanglist(prodaddress,$("#prodaddress"),"prodaddress")//地址
	},
	intiChange:function(){
		$("#peopletype span").each(function(i,v){
			if ($(this).attr("attrid")==common.Geturl().people_style) {
				$(this).addClass("on");
			};
		});

		$("#brands span").each(function(i,v){
			if ($(this).attr("attrid")==common.Geturl().brand) {
				$(this).addClass("on");
			};
		});
		$("#goodstyle span").each(function(i,v){
			if ($(this).attr("attrid")==common.Geturl().goods_style) {
				$(this).addClass("on");
			};
		});
		$("#prodaddress span").each(function(i,v){
			if ($(this).attr("attrid")==common.Geturl().prodaddress) {
				$(this).addClass("on");
			};
		});
	},
	DaohangeventFun:function(){
			var flag=1;
				brandFun.goodsListObj.people_style=common.Geturl().people_style;
				brandFun.goodsListObj.token=token,
				brandFun.goodsListObj.seller_id=(/seller_id/.test(window.location.href))?(common.Geturl().seller_id):(common.getCookie("sellerid"));
				brandFun.goodsListObj.ordercloum=$("#sortNav li").eq(0).attr("attrC");

		$(".mores").on("click",function(){
			$(this).parents("li").toggleClass("open");
		});
		$("#peopletype span").each(function(i,v){
			if ($(this).attr("attrid")==common.Geturl().people_style) {
				$(this).addClass("on");
			};
		});
		$("#brands span").each(function(i,v){
			if ($(this).attr("attrid")==common.Geturl().brand) {
				$(this).addClass("on");
			};
		});
		

		$("#peopletype").on("click","span",function(){
			//$(".goodstyle").hide();
			brandFun.goodsListObj.people_style=$(this).attr("attrid");
			//brandFun.goodsListObj.brand=common.Geturl().brand;
			var seller_id=(/seller_id/.test(window.location.href))?(common.Geturl().seller_id):(common.getCookie("sellerid"));

		common.ajaxFun(brandFun.Interface.goodsDaohang,"POST",{people_style:$(this).attr("attrid"),token:token,seller_id:seller_id},brandFun.initdaohang);   //导航
		$(this).addClass("on").siblings("span").removeClass("on");
		brandFun.changeUrl($(this).attr("key"),$(this).attr("attrid"),1);

		//common.ajaxFun(brandFun.Interface.brandHomepage,"POST",brandFun.goodsListObj,brandFun.goodsListFun);   //适应人群
		});
		$("#goodstyle").on("click","span",function(){
			$(this).addClass("on").siblings("span").removeClass("on");
			var brand=$("#brands").children("span.on").attr("attrid");
			brandFun.goodsListObj.goods_style=$(this).attr("attrid");
			brandFun.goodsListObj.brand=brand;
			brandFun.changeUrl($(this).attr("key"),$(this).attr("attrid"));

		//common.ajaxFun(brandFun.Interface.brandHomepage,"POST",brandFun.goodsListObj,brandFun.goodsListFun);   //风格
		});
		$("#brands").on("click","span",function(){
			$(this).addClass("on").siblings("span").removeClass("on");
			brandFun.goodsListObj.brand=$(this).attr("attrid");
			brandFun.changeUrl($(this).attr("key"),$(this).attr("attrid"));
		//common.ajaxFun(brandFun.Interface.brandHomepage,"POST",brandFun.goodsListObj,brandFun.goodsListFun);   //品牌
		});
		$("#prodaddress").on("click","span",function(){
			$(this).addClass("on").siblings("span").removeClass("on");
			brandFun.goodsListObj.prodaddress=$(this).attr("attrid");
			brandFun.changeUrl($(this).attr("key"),$(this).attr("attrid"))
		//common.ajaxFun(brandFun.Interface.brandHomepage,"POST",brandFun.goodsListObj,brandFun.goodsListFun);   //产地
		});
		
		
		$("#sortNav").on("click","li.lis",function(){
			$(this).addClass("on").siblings("li").removeClass("on");
			brandFun.goodsListObj.ordercloum=$(this).attr("attrc");
			if (brandFun.goodsListObj.ordercloum==4) {
				if (flag==1) {
					$(".price").addClass("priceUp").removeClass("priceDown");
					flag=0;
					brandFun.goodsListObj.ordertype="desc";
				}else if (flag==0) {
					$(".price").addClass("priceDown").removeClass("priceUp");
					flag=1;
					brandFun.goodsListObj.ordertype="asc";
				};
				
			};
			testn=1;
			brandFun.goodsListObj.page=1;
		common.ajaxFun(brandFun.Interface.brandHomepage,"POST",brandFun.goodsListObj,brandFun.goodsListFun);   //产地

		});
		$("#region2 p").click(function(){
			$("#regionList2").show();
			$("#provinceList2").hide();
			$(this).addClass("open");
		})
		$("#province2 p").click(function(){
			common.ajaxFun(commonFunObj.Interface.getProvince,"POST",{},function(res){
				var strProv="",v;
				$.each(res.data,function(k,v){
					strProv+="<div id="+k+">"+v+"</div>";
				})
				$("#provinceList2").html(strProv);
					$("#provinceList2").show();
					$("#regionList2").hide();
					$(this).addClass("open");
				
				});
			}); 
				$("#provinceList2").on("click","div",function(){
					va=$(this).attr("id");
					goodsListObj.province=va;
					$("#region2 p").html("请选择市").addClass("open");
					$("#province2 p").attr("value",va);
					$("#province2 p").html($(this).text()).removeClass("open");
					
					$(this).addClass("on").siblings("div").removeClass("on");
					$("#provinceList2").hide();
					common.ajaxFun(brandFun.Interface.brandHomepage,"POST",goodsListObj,brandFun.goodsListFun);   //省份

					common.ajaxFun(commonFunObj.Interface.getCity,"POST",{provinceid:va},function(res){
						$("#regionList2").show();
						var strCity="",v;
							$.each(res.data,function(k,v){
								strCity+="<div id="+k+">"+v+"</div>";
							});
							$("#regionList2").html(strCity);
						}); 
				});
				$("#regionList2").on("click","div",function(){
					$("#region2 p").html($(this).text()).removeClass("open");
					$("#region2 p").attr("value",$(this).attr("id"));
					goodsListObj.city=$(this).attr("id");
					$("#regionList2").hide();
					$(this).addClass("on").siblings("div").removeClass("on");
					common.ajaxFun(brandFun.Interface.brandHomepage,"POST",goodsListObj,brandFun.goodsListFun);   //城市

				})
			
		
	},
	DetailsFun:function(){
		$("#clothesMain").on("click","li",function(){
			var goods_id=$(this).attr("goods_id");
			//common.removeCookie();
			//common.removeCookie("sellerid");
			window.location.href="../page/CommodityDetails.html?goods_id="+goods_id;
		})
	},
	goodsListFun:function(res){
			//console.log(res.data)
				if (res.code==1) {
					str="";
				}else {
				var str="";
					$.each(res.data,function(i,v){
						if (typeof v=="object") {
						str+='<li goods_style='+v.goods_style+' goods_id='+v.goods_id+'>'+
							'<div class="imgBox" people_style='+v.people_style+'><img src='+v.image+' alt=""></div>'+
							'<div class="prices">¥'+v.min_price+'</div>'+
							'<div class="introduce">'+v.goods_name+'</div>'+
							'<div class="company" seller_id='+v.seller_id+' brand='+v.brand+'>'+v.seller_name+'</div>'+
							'<div class="productionAddress" prodaddress='+v.prodaddress+'>'+v.shipaddress_name+'</div>'+
						'</li>';
						}
					});
				};
				$("#clothesMain ul").html(str);
				//console.log(brandFun.pageFlag);
					brandFun.pageFun(res,brandFun.goodsListObj);
			},
		pageFun:function(res,Obj){
			var pagelimit=Obj.pagelimit;//每页条数
			//console.log(res.data.count + "totalAcounts")
			pageinit(res.data.count,Math.ceil(res.data.count/pagelimit));
			
			function pageinit(totalRecords,totalPage){

				var totalPage = totalPage;//总页数
				var totalRecords = totalRecords;    //总数据条数
				var pageNumber = testn;

				if(!testn){
					pageNumber = 1;
				}
				console.log("总数据条数："+totalRecords+"总页数："+totalPage)
				//生成分页
				//有些参数是可选的，比如lang，若不传有默认值
				kkpager.generPageHtml({
					pno : pageNumber,
					//总页码
					total : totalPage,
					//总数据条数
					totalRecords : totalRecords,
					isGoPage 			: false,	//是否显示页码跳转输入框
					isWrapedPageBtns	: false,	//是否用span包裹住页码按钮
					isShowFirstPageBtn	: false, //是否显示首页按钮
					isShowLastPageBtn	: false, //是否显示尾页按钮
					mode : 'click',//默认值是link，可选link或者click
					click : function(n){
						testn = n;
						Obj.page=n;
						console.log(testn)
						common.ajaxFun(brandFun.Interface.brandHomepage,"POST",Obj,function(res){
							brandFun.goodsListFun(res);
						}); 
						this.selectPage(n);
						return false;
					}
				},true);
			};
		}

}	
brandFun.inits();